<?php

namespace App\Admin\Extensions;

use Encore\Admin\Form\Field;

class ShowImg extends Field
{
    protected $view = "admin.showimg";

    protected static $css = [];

    protected static $js = [];

    public function render()
    {
        $name = $this->formatName($this->column());

        $host = config('app.url ');
        $this->script = <<<EOT
        // 显示广告图
        function showImg(){       
            var position_id = $('.position_id').val();
            var url = "{$host}/admin/api/activity/img/" + position_id
            var data = {}
            $.get(url, data, function (e){
                $('#showImg').attr('src', "{$host}/uploads/" + e.subject_url)
            }, 'json')
        }
        
        // 同步广告
        function synchron(){
            var clients_id = $('.clients_id').val();
            var url = "{$host}/admin/api/activity/synchron/" + clients_id
            var data = {}
            $.get(url, data, function (data){
                $('#official').val(data.official)
                $('#button_official').val(data.button_official)
                $('#odds').val(data.odds)
                $('#put_city').val(data.put_city)
                $('input[name="starttime"]').val(data.starttime)
                $('input[name="endtime"]').val(data.endtime)
                
                // par = $('.file-input.file-input-new').parent()
                // imgurl = data.imgurl
                // str = `<input name="imgurl" type="text" class="imgurl2" value="\${imgurl}" style="display: none;" />`
                // par.append(str)
            }, 'json')
        }
        
        $('.clients_id').change(function (){
            synchron()
        })
        
        $('.position_id').change(function (){
            showImg()
        })
        
        setTimeout(function (){
            showImg()
        }, 1000)

EOT;


        return parent::render(); // TODO: Change the autogenerated stub
    }
}